package cn.wdm.org.service.impl;

import cn.wdm.basic.exception.BusinesExceprion;
import cn.wdm.basic.query.BaseQuery;
import cn.wdm.basic.util.BaiduAuditUtils;
import cn.wdm.basic.util.PageList;
import cn.wdm.org.domain.Employee;
import cn.wdm.org.domain.Shop;
import cn.wdm.org.domain.ShopAuditLog;
import cn.wdm.org.mapper.EmployeeMapper;
import cn.wdm.org.mapper.ShopAuditLogMapper;
import cn.wdm.org.mapper.ShopMapper;
import cn.wdm.org.service.IShopService;
import cn.wdm.basic.service.impl.BaseServiceImpl;
import cn.wdm.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author laozhang
 * @since 2022-06-12
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    /**
     *1.校验信息是否为空
     * 2.合法校验
     * 3.判断店铺是否存在
     *    3.1 店铺不存在则新增店铺
     *    3.2 店铺存在 滚
     *4.判断店长是否存在
     *     4.1 店长存在
     *       4.1.1 判断店长有店铺 滚
     *       4.1.2 店长没有店铺 给店长新增店铺 返回自增id，将店铺绑定给店长，并且将店长的id给店铺的admin-id
     *     4.2 店长不存在
     *       新增店长，将店铺绑定给店长
     *5.保存数据，保存shop和employee
     * @param shop
     */
    @Override
    public void settlement(Shop shop) {
        //把对象中的引用属性拿出来
        Employee admin = shop.getAdmin();
        //1.校验信息是否为空
        if (StringUtils.isEmpty(shop.getName())||StringUtils.isEmpty(shop.getAddress())
                                                ||StringUtils.isEmpty(admin.getUsername())
        ) {
            throw new BusinesExceprion("参数不能为空！");
        }
        //2.合法校验 如果不合法就进来
        if (!BaiduAuditUtils.TextCensor(shop.getName())) {
            throw new BusinesExceprion("骚年，你有点危险！");
        }
        //3.判断店铺是否存在，店铺不能被注册，如果店铺存在，则抛出异常，如果店铺不能存在，继续往下走
        Shop shopTemp=shopMapper.loadShopByName(shop.getName());
        if (shopTemp != null) {
            throw new BusinesExceprion("店铺已经存在！");
        }


        //4.判断店长是否存在，店长不存在，则直接新增和绑定，如果店长存在，看是否有shop，有shop，抛异常，
        Employee employeeTem=employeeMapper.loadEmployeeByUsername(admin.getUsername());
            //4.1店长存在 但是店长绑定了店铺
        if (employeeTem != null&&employeeTem.getShop_id()!=null) {
            throw new BusinesExceprion("该成员已经存在，并且已经绑定了商铺，不能入驻");
        }
            //4.2 店长存在 但是店长没有绑定店铺，先新增店铺后面修改店长中的店铺id
            //4.3 店长人都不存在 直接新增店长，返回店长的自增id，然后赋值给临时的变量，后面要用。
        if(employeeTem == null){
            employeeMapper.save(admin);//要返回自增ID
            employeeTem = admin;
        }
        //保存数据
        //报存店铺 返回自增的店铺id，在shop中
        shop.setAdminId(employeeTem.getId());
        shopMapper.save(shop);
        //设置店长的shopid，同时修改店长中的shopid
        employeeTem.setShop_id(shop.getId());
        employeeMapper.update(employeeTem);
    }
    /**
     * 店铺审核通过
     * @param shopAuditLog
     */
    @Override
    public void pass(ShopAuditLog shopAuditLog) {
        //1.修改店铺的状态，审核通过 1表示通过
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        shop.setState(1);
        shopMapper.update(shop);
        //2.记录日志 @TODO 还要保存当前审核人
        shopAuditLog.setAuditId(339L);
        shopAuditLog.setState(1);
        shopAuditLogMapper.save(shopAuditLog);
        //3.发送邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("1756024408@qq.com");
        //邮件主题
        mailMessage.setSubject("审核通过");
        //邮件内容
        mailMessage.setText("审核通过 ，请尽快激活！http://localhost:8081/#/register2/"+shop.getId());
        //查找当前店铺的店长 这里容易空指针异常，当新增店铺的时候，
        Employee employee=employeeMapper.loadEmpByShopId(shopAuditLog.getShopId());
        //收件人
        mailMessage.setTo(employee.getEmail());
        javaMailSender.send(mailMessage);
    }
    /**
     * 店铺审核失败
     * @param shopAuditLog
     */
    @Override
    public void reject(ShopAuditLog shopAuditLog) {
        //1.修改店铺的状态为2 驳回
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        shop.setState(2);
        shopMapper.update(shop);

        //2.记录日志 @TODO 还要保存当前审核人
        shopAuditLog.setAuditId(339L);
        shopAuditLog.setState(2);
        shopAuditLogMapper.save(shopAuditLog);
        //3.发送邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("1756024408@qq.com");
        //邮件主题
        mailMessage.setSubject("店铺信息驳回");
        //邮件内容
        mailMessage.setText("你填写的信息有误");
        //查找当期店铺的店长
        Employee employee=employeeMapper.loadEmpByShopId(shopAuditLog.getShopId());
        //收件人
        mailMessage.setTo(employee.getEmail());
        javaMailSender.send(mailMessage);
    }
    /**
     * 批量新增
     * @param shops
     */
    @Override
    public void savaBatch(List<Shop> shops) {
        shopMapper.saveBatchShop(shops);
    }
    /**
     * 获取echarts报表
     * @return
     */
    @Override
    public List<ShopVo> getEcharts() {
        return shopMapper.loadEcharts();
    }


}
